package com.jinjiafeng.baselibrary.widget.recycleview;

import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author jinjiafeng
 * Time :18-6-10
 */
@SuppressWarnings({"UnusedReturnValue", "WeakerAccess", "unused"})
public class BaseViewHolder extends RecyclerView.ViewHolder {

    private final SparseArray<View> mViewArray;
    private final View mItemView;

    public BaseViewHolder(View itemView) {
        super(itemView);
        mItemView = itemView;
        mViewArray = new SparseArray<>();
    }


    public BaseViewHolder setText(@IdRes int id, CharSequence text) {
        TextView textView = getView(id);
        textView.setText(text);
        return this;
    }

    public BaseViewHolder setText(@IdRes int id, @StringRes int  resId) {
        TextView textView = getView(id);
        textView.setText(resId);
        return this;
    }

    public BaseViewHolder setImageResource(@IdRes int id, @DrawableRes int  resId) {
        ImageView imageView = getView(id);
        imageView.setImageResource(resId);
        return this;
    }

    public <T extends View> T getView(@IdRes int id) {
        View view = mViewArray.get(id);
        if (view == null) {
            view = mItemView.findViewById(id);
            mViewArray.put(id, view);
        }
        //noinspection unchecked
        return (T) view;
    }
}
